﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace coliseu.BussinessLayer
{
    public class ApartamentoBL
    {
          private coliseuEntities cEntities;

          public ApartamentoBL()
        {
              
            cEntities = new coliseuEntities(Properties.Settings.Default.conn_coliseu);
        }

          public List<apartamento> selecionar()
        {
            return (from x in cEntities.apartamento select x).ToList();
        }

          public List<apartamento> selecionar(string bloco)
          {
              return (from x in cEntities.apartamento where x.bloco.id_bloco == bloco select x).ToList();
          }

          public List<apartamento> selecionar(string descricao, string bloco)
        {
              if(!string.IsNullOrEmpty(descricao) && !string.IsNullOrEmpty(bloco))
                  return (from x in cEntities.apartamento where x.numero.Contains(descricao) && x.id_bloco.Contains(bloco) select x).ToList();
              else
                  if(!string.IsNullOrEmpty(descricao))
                       return (from x in cEntities.apartamento where x.numero.Contains(descricao) select x).ToList();
                  else
                      return (from x in cEntities.apartamento where x.id_bloco.Contains(bloco) select x).ToList();
        }

        public void salvar(apartamento b, string tipo)
        {

            if (tipo == "insert")
            {
                cEntities.AddToapartamento(b);
            }
            else
            {
                cEntities.apartamento.ApplyCurrentValues(b);
            }
            cEntities.SaveChanges();
        }

        public void excluir(apartamento b)
        {
            cEntities.apartamento.DeleteObject(b);
            cEntities.SaveChanges();
        }
    }
}
